package com.example;


import com.aizuda.monitor.OshiMonitor;
import com.example.entity.Monitor;
import com.example.entity.Usestaff;
import com.example.service.IMonitorService;
import com.example.service.IUsestaffService;
import com.example.test.Screan;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.time.LocalDateTime;
import java.util.*;
import java.util.List;

@SpringBootApplication
@EnableOpenApi
@ComponentScan("com.example")
@MapperScan("com.example.mapper")
class DartApplication implements CommandLineRunner {
    @Autowired
    private OshiMonitor oshiMonitor;
    @Autowired
    private IUsestaffService iUsestaffService;
    @Autowired
    private IMonitorService iMonitorService;

    @Override
    public void run(String... args) throws AWTException, IOException, InterruptedException {
        while (iUsestaffService.list().get(0).getStaffid() != 0) {
            InetAddress address=InetAddress.getLocalHost();
            String name=address.getHostName();
            LocalDateTime checktime = LocalDateTime.now();
            int staffid = iUsestaffService.lambdaQuery().eq(Usestaff::getName,name).list().get(0).getStaffid();
            int limitNumber = 10;
            List<OSProcess> processList = this.oshiMonitor.getOperatingSystem().getProcesses(
                    null, OperatingSystem.ProcessSorting.CPU_DESC, limitNumber
            );
            List<Monitor> list = new ArrayList<>();
            for (OSProcess process : processList) {
                Screan screan=new Screan();

                Monitor monitor = new Monitor();
                monitor.setStaffid(staffid);
                monitor.setProcessname(process.getName());
                monitor.setPid(process.getProcessID());
                monitor.setStarttime(process.getStartTime());
                monitor.setChecktime(checktime);
                long time = System.currentTimeMillis() - process.getStartTime();
                monitor.setUsetime(time);
                monitor.setCpu((float) this.oshiMonitor.formatDouble(process.getProcessCpuLoadCumulative()));
                iMonitorService.save(monitor);
                monitor.setPicture(screan.picture(monitor.getId()));
                iMonitorService.updateById(monitor);
                list.add(monitor);
            }
            Thread.sleep(30000);
        }

    }
}

public class App {
//    public static void main(String[] args) {
//        SpringApplication.run(DartApplication.class, args);
//    }
public static void main(String[] args) {
    SpringApplicationBuilder builder=new SpringApplicationBuilder(DartApplication.class);
    builder.headless(false).run(args);
}
}